--[[
 -- *名称:RecordLayer
 -- *描述:牌局记录
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-10-16
 -- *修改日期:2017-10-16
 ]]
local CURRENT_MODULE_NAME = ...
local RecordLayer = class("RecordLayer", PokerViewBase)


--[[
 界面处理需要保证就算是错误数据也做到不崩溃
 在获取到数据的时候进行checkData操作
]]
function RecordLayer:onCreate()
    self:initWithFilePath("recordlayer", CURRENT_MODULE_NAME);
    
    local scale = display.width / 1280;
    self.node.bg:setScale(scale);
    AnimUtils.playPopupAnim(self.node.bg);

    self:_initialize();
end

--返回键事件
function RecordLayer:backEvent()
    RecordLayer.super.backEvent(self);
end

function RecordLayer:_initialize(  )
	self._playerItemLayout = self.node.player_item_layout;
	self._recordItemLayout = self.node.record_item_layout;
    self._recordItemLayout:setTouchEnabled(false); 

	self._listVertical = self.node.listview_vertical;
	self._listVertical:setItemModel(self._recordItemLayout);

	self._lblWaiting = self.node.lbl_waiting;
end
---
 -- 刷新界面
 -- @DateTime 2017-11-10
 -- @param    data [description]
 -- @return    [description]
 --
function RecordLayer:refreshView( data )
	if not data or #data == 0 then 
		self._lblWaiting:setString("无相关战绩记录！");
		self._lblWaiting:hide();
		return;
	end
	self._lblWaiting:show();
	Log.d("RecordLayer:refreshView #data: ", #data);
	self._data = data;
	if self._refreshViewEntryId then 
		FrameUtils.removeEnterFrameListener(self._refreshViewEntryId);
		self._refreshViewEntryId = nil;
	end
	self._listVertical:hide();
	local thread_i =coroutine.create(function ()
		for i = 1, #data do 
			self._listVertical:insertDefaultItem(i - 1);
			local recordItemLayout = self._listVertical:getItem(i - 1);

			local recordItemData = data[i];
			-- 更新房间号
			local lblRoomId = recordItemLayout:getChildByName("room_id_text");
			lblRoomId:setString(recordItemData.tableIndex);
			-- 更新支付类型
			local lblPayType = recordItemLayout:getChildByName("pay_text");
			lblPayType:setString(recordItemData.payType == 0 and "房主支付" or "AA支付");
			-- 更新局数
			local lblRounds = recordItemLayout:getChildByName("round_text");
			lblRounds:setString(string.format("%s/%s", recordItemData.handNum, recordItemData.totalHandNum));
			-- 更新时间
			local lblTime = recordItemLayout:getChildByName("time_text");
			lblTime:setString(recordItemData.startTime);

			-- 刷新玩家列表
			local listHorizontal = recordItemLayout:getChildByName("listView_horizontal");
			listHorizontal:setScrollBarEnabled(false);
			listHorizontal:setItemModel(self._playerItemLayout);
			local allPlayers = data[i].players;
			for i = 1, #allPlayers do 
				listHorizontal:insertDefaultItem(i - 1);
				local playerItemLayout = listHorizontal:getItem(i - 1);

				local player = allPlayers[i];
				-- 更新昵称
				local lblName = playerItemLayout:getChildByName("name");
				lblName:setString(string.SubPlayerName(player.playerName));
				-- 更新玩家ID
				local lblId = playerItemLayout:getChildByName("id");
				lblId:setString("ID: " .. player.playerId);
				-- 更新玩家分数
				local lblInsScore = playerItemLayout:getChildByName("score_ins");
				local lblDesScore = playerItemLayout:getChildByName("score_des");

				if player.score > 0 then 
					lblInsScore:setString("." .. player.score);
					lblInsScore:show();
					lblDesScore:hide();
				else 
					lblDesScore:setString("/" .. player.score);
					lblDesScore:show();
					lblInsScore:hide();
				end
				local headBg = playerItemLayout:getChildByName("head_bg");
				-- 更新玩家头像
				local headSprite = cc.Sprite:create(self.res_base .. "/default_head.png");
				headSprite:setScale(1.6);
				headSprite:setPosition(cc.p(94, 94));
				headBg:addChild(headSprite);
				if not string.isNullOrEmpty(player.headImgUrl) and headSprite then 
					Log.d("Loading head image...");
					Util.loadHeadImg(player.headImgUrl, headSprite);
				end

				-- 更新房主标识 
				local imgCreator = headBg:getChildByName("img_creator");
				imgCreator:setLocalZOrder(1);
				if data[i].hostId == player.playerId then 
					imgCreator:show();
				else 
					imgCreator:hide();
				end
			end
			coroutine.yield();
		end
		self._listVertical:show();
		self._lblWaiting:hide();
	end);

	self._refreshViewEntryId = FrameUtils.addEnterFrameListener(function() 
		if coroutine.status(thread_i)=='dead' then
			FrameUtils.removeEnterFrameListener(self._refreshViewEntryId);
			return
		end
		coroutine.resume(thread_i)
	end, 0);
end

return RecordLayer